﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Loot
{
	public partial class Form1 : Form
	{
		public string Filename { get; set; }

		public Form1()
		{
			DialogResult = DialogResult.No;
			Filename = "";
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			bindRecent();
		}

		public string RecentData
		{
			get
			{
				object o = Registry.GetValue("HKEY_CURRENT_USER\\Software\\Possan\\Loot translator", "MRU", "");
				if (o != null)
					return o.ToString();
				return "";
			}
			set
			{
				Registry.SetValue("HKEY_CURRENT_USER\\Software\\Possan\\Loot translator", "MRU", value.ToString());
			}
		}

		void bindRecent()
		{
			listBox1.Items.Clear();
			string[] mrus = RecentData.Split('\n');
			foreach (string s in mrus)
				if (!string.IsNullOrEmpty(s))
					listBox1.Items.Add(s);
		}

		void touchRecent(string filename)
		{
			List<string> mru = new List<string>();

			string[] tmp = RecentData.Split('\n');
			foreach (string s in tmp)
				if (!string.IsNullOrEmpty(s))
					mru.Add(s);

			if (!string.IsNullOrEmpty(filename))
			{
				if (mru.Contains(filename))
					mru.Remove(filename);

				mru.Insert(0, filename);
			}

			RecentData = string.Join("\n", mru.ToArray()).Trim();
		}

		private void buBrowse_Click(object sender, EventArgs e)
		{
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Title = "Select one or more language files...";
			ofd.Filter = "Language files (*.lxml)|*.lxml|All files (*.*)|*.*";
			ofd.Multiselect = true;
			DialogResult ofddr = ofd.ShowDialog();
			if (ofddr == DialogResult.OK)
			{
				touchRecent(ofd.FileName);
				DialogResult = DialogResult.OK;
				Filename = ofd.FileName;
				Close();
				// add to recent
			}
		}

		private void buMerge_Click(object sender, EventArgs e)
		{
			FileMerger fm = new FileMerger();

			bool morefiles = true;
			bool domerge = true;

			while (morefiles)
			{
				Application.DoEvents();

				OpenFileDialog ofd = new OpenFileDialog();
				ofd.Title = "Select one or more language files...";
				ofd.Filter = "Language files (*.lxml)|*.lxml|All files (*.*)|*.*";
				ofd.Multiselect = true;
				DialogResult ofddr = ofd.ShowDialog();
				if (ofddr == DialogResult.OK)
				{
					foreach (string s in ofd.FileNames)
						fm.AddFile(s);

					DialogResult dr = MessageBox.Show("Merge more files?", "Continue", MessageBoxButtons.YesNoCancel);
					if (dr == DialogResult.No)
					{
						morefiles = false;
					}
					else if (dr == DialogResult.Cancel)
					{
						morefiles = false;
						domerge = false;
					}
				}
				else
				{
					morefiles = false;
					domerge = false;
				}
			}

			if (domerge)
			{
				SaveFileDialog sfd = new SaveFileDialog();
				sfd.Title = "Save merged language file as...";
				sfd.Filter = "Language files (*.lxml)|*.lxml|All files (*.*)|*.*";
				sfd.FileName = "Merged.lxml";
				DialogResult sfddr = sfd.ShowDialog();
				if (sfddr == DialogResult.OK)
				{
					fm.Merge(sfd.FileName);
					touchRecent(sfd.FileName);
					// add to recent...
				}
			}
			bindRecent();
		}

		private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (listBox1.SelectedIndex != -1)
			{
				Filename = listBox1.Items[listBox1.SelectedIndex].ToString();
				touchRecent(Filename);
				DialogResult = DialogResult.OK;
				Close();
			}
		}
	}
}
